﻿using UnityEngine;
using System.Collections;

public class InputController : MonoBehaviour {
	
	public GUITexture gui_buttonA;
	public GUITexture gui_buttonB;
	public GUITexture gui_joystickBG;
	public GUITexture gui_joystick;
	
	public Vector2 startTouch = Vector2.zero; // Initialise the values to zero
	
	public GameObject prefab;

	// Use this for initialization
	void Start () {		
	}
	
	// Update is called once per frame
	void Update () {
		
		if(Input.touchCount < 1) return;
		
//		// Find the game object
//		GameObject cube = GameObject.Find ("Cube");
		
		GameObject[] interactiveObjs = GameObject.FindGameObjectsWithTag ("Interactive");
		
		// Get all the touches registered
		foreach(Touch touch in Input.touches)
		{
			foreach(GameObject obj in interactiveObjs)
				obj.SendMessage ("HandleTouchInput", touch);
			
//			switch(touch.phase)
//			{
//			case TouchPhase.Began:	// Handles the start of touch
//				
//				// Get the Ray object based on the position of touch
//				Ray ray = Camera.main.ScreenPointToRay(touch.position);
//				
//				RaycastHit hit; // store the information of object selected
//				
//				if(Physics.Raycast(ray, out hit)) // if raycase hits an object
//				{
//					if(hit.collider.gameObject == cube) // if object hit is the cube
//						cube.renderer.material.color = Color.red;
//				}
//				
//				cube.renderer.material.color = Color.red;
//				
//				// Get the position where touch begins
//				startTouch = touch.position;
//				
//				// Create a spawn position based on touch position
//				Vector3 spawnPos = touch.position;
//				spawnPos.z = 10; // 10 unit into the screen from camera position
//				
//				// Convert from screen position to world position
//				Vector3 worldPos = camera.main.ScreenToWorldPoint (spawnPos);
//				
//				// Instantiate the object at position and default rotation
//				GameObject obj = Instantiate (prefab, worldPos, Quaternion.identity) as GameObject;
//				obj.name = "Cube 2";
//				obj.tag = "Interactive";
//				
//				break;
//				
//			case TouchPhase.Ended:	// Handles touch is released
//				cube.renderer.material.color = Color.white;
//				
//				// Find the directino when touch ends
//				Vector2 swipeDir = touch.position - startTouch;
//				
//				if(swipeDir.x > 0) // moving to the right side?
//					cube.transform.Rotate (0, -10, 0);
//				else if(swipeDir.x < 0)
//					cube.transform.Rotate (0, 10, 0);
//				
//				break;
//				
//			case TouchPhase.Moved:	// Handles touch has moved
//				
//				// Find the difference in position from the last touch
//				Vector2 dir = touch.deltaPosition;
//			
//				if(dir.x > 0) cube.transform.Rotate(0, -1, 0);
//				else if(dir.x < 0) cube.transform.Rotate (0, 1, 0);
//				
//				break;
//				
//			case TouchPhase.Stationary:	// Handles press and hold at a position
//				break;
//			}
//			
//			// Check if gui_buttonA is touched at give touch position
//			if( gui_buttonA.HitTest(touch.position) )
//			{
//				if(touch.phase == TouchPhase.Ended) continue;
//				
//				// Find the game object in the scene
//				GameObject cube = GameObject.Find ("Cube");
//				
//				// Rotate the object to the right
//				cube.transform.Rotate(0, -1, 0, Space.Self);
//			}
//			
//			// Check if gui_buttonB is touched at give touch position
//			else if( gui_buttonB.HitTest(touch.position) )
//			{
//				if(touch.phase == TouchPhase.Ended) continue;
//				
//				// Find the game object in the scene
//				GameObject cube = GameObject.Find ("Cube");
//				
//				// Rotate the object to the left
//				cube.transform.Rotate(0, 1, 0, Space.Self);
//			}
//			
//			// If touch is in the boundary of joystick
//			else if( gui_joystickBG.HitTest(touch.position) )
//			{
//				if(touch.phase == TouchPhase.Ended)
//				{
//					gui_joystick.transform.position = gui_joystickBG.transform.position;
//				}
//				else
//				{
//					// Get touch position in GUITexture coordinate (0,0) to (1,1)
//					Vector3 touchPos = new Vector3(	touch.position.x / Screen.width,
//													touch.position.y / Screen.height,
//													0 );
//					
//					// Get Direction
//					Vector3 dir = new Vector3(	touchPos.x - gui_joystickBG.transform.position.x,
//												touchPos.y - gui_joystickBG.transform.position.y,
//												0.0f );
//					
//					// Move the joystick button to where the touch position is
//					gui_joystick.transform.position = gui_joystickBG.transform.position + dir * 0.5f;
//					
//					// Find the game object in the scene
//					GameObject cube = GameObject.Find ("Cube");
//					
//					// Move the cube around
//					cube.transform.Translate(dir * 0.5f);
//				}
//
//				Vector3 temp = gui_joystick.transform.position;
//				temp.z = 1;
//				gui_joystick.transform.position = temp;
//			}
		}
		
	}
}
